import { http } from '@/api/request'

function getMaxStep(startTime: number, endTime: number) {
  const duration = endTime - startTime
  return Number(duration / 10000)
}

function getObjectList(param: any) {
  const queryKeyword = param.queryKeyword
  const startTime = param.startTime
  const endTime = param.endTime
  const step = getMaxStep(startTime, endTime)
  return http.get(
    `/prometheus/api/v1/query_range?query=
        sum(gala_gopher_l7_resp_count{tgid=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_l7_resp_count{comm=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_l7_resp_count{container_name=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_l7_resp_count{pod=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_proc_fd_count{comm=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_proc_fd_count{tgid=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job) or
        sum(gala_gopher_container_start_time_seconds{container_name=~".*${queryKeyword}.*"})
        by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name, job)
        &start=${startTime}&end=${endTime}&step=${step}`,
    {},
  )
}

export function getApiList(param) {
  var startTime = param.startTime;
  var endTime = param.endTime;
  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query_range?query=
            sum(gala_gopher_l7_api_req_count{pod_id=~"${param.objectId}"})by(api) or sum(gala_gopher_l7_api_resp_count{pod_id=~"${param.objectId}"})by(api)
            &start=${startTime}&end=${endTime}&step=15`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query_range?query=
            sum(gala_gopher_l7_api_req_count{container_id=~"${param.objectId}"})by(api) or sum(gala_gopher_l7_api_resp_count{container_id=~"${param.objectId}"})by(api)
            &start=${startTime}&end=${endTime}&step=15`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query_range?query=
            sum(gala_gopher_l7_api_req_count{tgid=~"${param.objectId}"})by(api) or sum(gala_gopher_l7_api_resp_count{tgid=~"${param.objectId}"})by(api)
            &start=${startTime}&end=${endTime}&step=15`,
        {});
  }
}

export function getGrafanaServerAddr() {
  return http.get('/grafanaServerAddr');
}

export function getClientReqCount(param) {
  const timestamp = param.timestamp / 1000;
  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_req_count{
                pod_id=~"${param.objectId}", l7_role=~"client"}[5m]))&time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_req_count{
                container_id=~"${param.objectId}", l7_role=~"client"}[5m]))&time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_req_count{
                tgid=~"${param.objectId}", l7_role=~"client"}[5m]))&time=${timestamp}`,
        {});
  }
}


export function getServerRespCount(param) {
  const timestamp = param.timestamp / 1000;
  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_resp_count{
                pod_id=~"${param.objectId}", l7_role=~"server"}[5m]))&time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_resp_count{
                container_id=~"${param.objectId}", l7_role=~"server"}[5m]))&time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=sum(sum_over_time(gala_gopher_l7_resp_count{
                tgid=~"${param.objectId}", l7_role=~"server"}[5m]))&time=${timestamp}`,
        {});
  }
}

function getQueryPatternByObjectType(param) {
  switch (param.type) {
    case 'pod':
      return `pod_id=~"${param.objectId}", machine_id=~".*${param.machine_id}.*"`;
    case 'container':
      return `container_id=~"${param.objectId}", machine_id=~".*${param.machine_id}.*"`;
    case 'proc':
      return `tgid=~"${param.objectId}", machine_id=~".*${param.machine_id}.*"`;
  }
}

export function getReqLatency(param) {
  var queryPattern = getQueryPatternByObjectType(param);
  const timestamp = param.timestamp / 1000;

  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"client", ${queryPattern}}[1m]))by(pod, pod_id) /
            (sum(sum_over_time(gala_gopher_l7_req_count{l7_role=~"client", ${queryPattern}}[1m]))by(pod, pod_id) != 0)
            &time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"client", ${queryPattern}}[1m]))by(container_id) /
            (sum(sum_over_time(gala_gopher_l7_req_count{l7_role=~"client", ${queryPattern}}[1m]))by(container_id) != 0)
            &time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"client",${queryPattern}}[1m]))by(tgid) /
            (sum(sum_over_time(gala_gopher_l7_req_count{l7_role=~"client", ${queryPattern}}[1m]))by(tgid) != 0)
            &time=${timestamp}`,
        {});
  }
}

export function getPodTgidByTcpMetric(param) {
  const timestamp = param.timestamp / 1000;
  return http.get(`/prometheus/api/v1/query?query=sum(gala_gopher_tcp_link_rx_bytes{pod_id=~"${param.objectId}"})
        by(tgid, machine_id)&time=${timestamp}`, {});
}

export function getRespLatency(param) {
  var queryPattern = getQueryPatternByObjectType(param);
  const timestamp = param.timestamp / 1000;

  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"server", ${queryPattern}}[1m]))by(pod, pod_id) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[1m]))by(pod, pod_id) != 0)
            &time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"server", ${queryPattern}}[1m]))by(container_id) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[1m]))by(container_id) != 0)
            &time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"server", ${queryPattern}}[1m]))by(tgid) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[1m]))by(tgid) != 0)
            &time=${timestamp}`,
        {});
  }
}

export function getServerErrRate(param) {
  var queryPattern = getQueryPatternByObjectType(param);
  const timestamp = param.timestamp / 1000;

  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_err_count{l7_role=~"server", ${queryPattern}}[5m]))by(pod, pod_id) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[5m]))by(pod, pod_id) != 0)
            &time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_err_count{l7_role=~"server", ${queryPattern}}[5m]))by(container_id) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[5m]))by(container_id) != 0)
            &time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=
            sum(sum_over_time(gala_gopher_l7_err_count{l7_role=~"server", ${queryPattern}}[5m]))by(tgid) /
            (sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server", ${queryPattern}}[5m]))by(tgid) != 0)
            &time=${timestamp}`,
        {});
  }
}

export function getTop3ReqRate(param) {
  var startTime = param.startTimestamp;
  var endTime = param.endTimestamp;
  var duration = parseInt(endTime - startTime);
  return http.get(`/prometheus/api/v1/query?query=
    topk(3, sum(sum_over_time(gala_gopher_l7_req_count{l7_role=~"client"}[${duration}s]))by(pod_id, container_id, tgid, machine_id, comm, container_name, pod, job) / ${duration})
    &time=${endTime}`,
    {});
}

export function getTop3ServerErrRate(param) {
  var startTime = param.startTimestamp;
  var endTime = param.endTimestamp;
  var duration = parseInt(endTime - startTime);
  return http.get(`/prometheus/api/v1/query?query=
    topk(3,
        sum(sum_over_time(gala_gopher_l7_err_count{l7_role=~"server"}[${duration}s]))by(pod_id, container_id, tgid, machine_id, comm, container_name, pod, job) /
        sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server"}[${duration}s]))by(pod_id, container_id, tgid, machine_id, comm, container_name, pod, job))
    &time=${endTime}`,
    {});
}

export function getTop3RespLatency(param) {
  var startTime = param.startTimestamp;
  var endTime = param.endTimestamp;
  var duration = parseInt(endTime - startTime);
  return http.get(`/prometheus/api/v1/query?query=
    topk(3,
        sum(sum_over_time(gala_gopher_l7_latency_sum{l7_role=~"server"}[${duration}s]))by(pod_id, container_id, tgid, machine_id, comm, container_name, pod, job) /
        sum(sum_over_time(gala_gopher_l7_resp_count{l7_role=~"server"}[${duration}s]))by(pod_id, container_id, tgid, machine_id, comm, container_name, pod, job))
    &time=${endTime}`,
    {});
}

export function getAppTcpLinkCount(param) {
  const timestamp = param.timestamp / 1000;

  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=
            sum(gala_gopher_tcp_link_srtt{pod_id=~"${param.objectId}"})by(client_ip, server_ip, server_port)
            &time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=
            sum(gala_gopher_tcp_link_srtt{container_id=~"${param.objectId}"})by(client_ip, server_ip, server_port)
            &time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=
            sum(gala_gopher_tcp_link_srtt{tgid=~"${param.objectId}"})by(client_ip, server_ip, server_port)
            &time=${timestamp}`,
        {});
  }
}

export function getAppTcpLinkSrtt(param) {
  const timestamp = param.timestamp / 1000;

  switch (param.type) {
    case 'pod':
      return http.get(`/prometheus/api/v1/query?query=
            gala_gopher_tcp_link_srtt{pod_id=~"${param.objectId}"}
            &time=${timestamp}`,
        {});
    case 'container':
      return http.get(`/prometheus/api/v1/query?query=
            gala_gopher_tcp_link_srtt{container_id=~"${param.objectId}"}
            &time=${timestamp}`,
        {});
    case 'proc':
      return http.get(`/prometheus/api/v1/query?query=
            gala_gopher_tcp_link_srtt{tgid=~"${param.objectId}"}
            &time=${timestamp}`,
        {});
  }
}

export function getAppTcpLinkCountByClientServer(client, server) {
  var timestamp = client.timestamp / 1000;
  return http.get(`/prometheus/api/v1/query?query=
        sum(gala_gopher_tcp_link_srtt{client_ip=~"${client.client_ip}", server_ip=~"${server.server_ip}", server_port=~"${server.server_port}"})by(client_ip, server_ip, server_port)
        &time=${timestamp}`,
    {});
}

export function getAppTcpLinkSrttByClientServer(client, server) {
  var timestamp = client.timestamp / 1000;
  return http.get(`/prometheus/api/v1/query?query=
        gala_gopher_tcp_link_srtt{client_ip=~"${client.client_ip}", server_ip=~"${server.server_ip}", server_port=~"${server.server_port}"}
        &time=${timestamp}`,
    {});
}

export function getL7ObjectList(param) {
  var queryKeyword = param.queryKeyword;
  var startTime = param.startTime;
  var endTime = param.endTime;
  var step = getMaxStep(startTime, endTime);
  return http.get(`/prometheus/api/v1/query_range?query=
        sum(gala_gopher_l7_resp_count{tgid=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name) or
        sum(gala_gopher_l7_resp_count{comm=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name) or
        sum(gala_gopher_l7_resp_count{container_name=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name) or
        sum(gala_gopher_l7_resp_count{pod=~".*${queryKeyword}.*"})by(tgid, container_id, pod_id, l7_role, machine_id, client_ip, server_ip, server_port, comm, pod, container_name)
        &start=${startTime}&end=${endTime}&step=${step}`,
    {});
}

export function getEsData(body) {
  var date = new Date();
  var esIndex = `aops_graph-${date.toISOString().split('T')[0]}`
  return http.post(`/es/${esIndex}/_search?pretty`, body);
}

export const appApis = {
  getObjectList,
  getGrafanaServerAddr,
  getApiList,
  getClientReqCount,
  getServerRespCount,
  getReqLatency,
  getPodTgidByTcpMetric,
  getRespLatency,
  getServerErrRate,
  getTop3ReqRate,
  getTop3ServerErrRate,
  getTop3RespLatency,
  getAppTcpLinkCount,
  getAppTcpLinkSrtt,
  getAppTcpLinkCountByClientServer,
  getAppTcpLinkSrttByClientServer,
  getL7ObjectList,
  getEsData
}
